<?php

$id = $_GET['id'];
$db = new Sektoral();
$table = "sektoral";
$order = "";
$sektoral = $db->lihat($table,$order,$id);

if ($sektoral) {
	
	$o .= '
			<form class="form-horizontal" ENCTYPE="multipart/form-data" id="registerHere" method="post" action="?page=editsektoral&action=edit&id='.$id.'">
				<fieldset>
					<legend>UBAH SEKTORAL</legend>';	
			
					if (isset($_GET['success']) && ($_GET['success'] == "1")) {
						$o .= '<div class="alert alert-success">
								Proses ubah sektoral berhasil
								</div>';			
					} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
						$o .= '<div class="alert alert-error">
								Proses ubah sektoral gagal
							   </div>';
					} elseif (isset($_GET['upload']) && ($_GET['upload'] == "0")) {
						$o .= '<div class="alert alert-error">
								Proses upload gambar gagal
							   </div>';
					}
	
	
	foreach ($sektoral as $x) {
		
		$o .= '
				<div class="control-group">
				  <label class="control-label" for="input01">Nama Sektoral</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" id="nama" value="'.$x['nama_sektoral'].'" name="nama" rel="popover" data-content="Masukkan nama sektoral." data-original-title="Nama Sektoral">
				  </div>
				</div>

				<div class="control-group">
					<label class="control-label" for="input01">Icon</label>
					<div class="controls">
						<input class="input-file" name="image" id="fileInput" type="file"  rel="popover" data-content="Masukan File Icon Berdasarkan Sektoral." data-original-title="Ikon" >
						<input type="hidden" name="icon_lama" value="'.$x['icon'].'">
					</div>
				</div>

				<div class="control-group">
					<label class="control-label" for="input01"></label>
					  <div class="controls">
					   <button type="submit" class="btn btn-success" >Ubah Sektoral</button>
					  </div>
				</div>
			  </fieldset>
			</form>';
	}
}  else {

$o .= '<div class="alert alert-error">
		Data tidak ditemukan
		</div>';
}

$o .= '<a href="?page=sektoral">kembali</a>';

echo $o;

if ((isset($_GET['action'])) && ($_GET['action'] == "edit")) {
	
	require ('../plugins/SimpleImage.php');
	$id = $_GET['id'];
	$dim  = array('W'=>32, 'H'=>37);
	$uploaddir 	= "../file/icon/$picName";
	$picName = basename($_FILES['image']['name']);

	if (is_uploaded_file($_FILES['image']['tmp_name'])) {
		if((copy($_FILES['image']['tmp_name'], $uploaddir.$picName))) {
			$imgType = IMAGETYPE_JPEG;
			$imgDms = getimagesize($_FILES['image']['tmp_name']);
			if ($imgDms[0] > $dim['W']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToWidth($dim['W']);
				$image->save($uploaddir.$picName, $imgType);
			} elseif ($imgDms[1] > $dim['H']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToHeight($dim['H']);
				$image->save($uploaddir.$picName, $imgType);
			}
		} else {
			echo '
			<script>
			document.location="?page=editsektoral&id='.$id.'&success=1";
			</script>';
		}	
		
		$datasektoral = array('nama_sektoral'=>$_POST['nama'],'icon'=>$picName);
		$editsektoral = $db->ubah($id,$table,$datasektoral);
				
		if ($editsektoral) {
			echo '
			<script>
			document.location="?page=editsektoral&id='.$id.'&success=1";
			</script>';
		
		} else {
			echo '
			<script>
			document.location="?page=editsektoral&id='.$id.'&success=0";
			</script>';
		}
	} else {

		$sektoral = array('nama_sektoral'=>$_POST['nama'],'icon'=>$_POST['icon_lama']);
		$editsektoral = $db->ubah($id,$table,$sektoral);
		
		if ($editsektoral) {
			echo '
			<script>
			document.location="?page=editsektoral&id='.$id.'&success=1";
			</script>';
		
		} else {
			echo '
			<script>
			document.location="?page=editsektoral&id='.$id.'&success=0";
			</script>';
		}
	}
}
?>




